package com.bonana.backend.controller;

import com.alibaba.fastjson.JSON;
import com.bonana.core.entity.CoreUserProfile;
import com.bonana.core.entity.mybatis.IndexProject;
import com.bonana.core.entity.mybatis.Project;
import com.bonana.core.entity.mybatis.ProjectActive;
import com.bonana.core.form.ProjectForm;
import com.bonana.core.result.Response;
import com.bonana.core.result.ResponseCode;
import com.bonana.core.services.BoBoService;
import com.bonana.core.services.ProjectService;
import com.bonana.core.utils.FileInfo;
import com.bonana.core.utils.OssUtils;
import com.bonana.utils.TimestampUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/7/5.
 */

@Controller
@RequestMapping(value="project")
public class ProjectController {
    @Autowired
    ProjectService projectService;
    @Autowired
    BoBoService boBoService;

    @RequestMapping(value="getProject", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo getBanner(Integer pgNum, Integer pgSize, @RequestParam(required = false) String query) {
        List<Project> list = projectService.getProjs(pgNum, pgSize, query);
        return new PageInfo(list);
    }

    @RequestMapping(value = "queryById")
    @ResponseBody
    public Project getProjectById(Integer projId) {
        Project bn = projectService.getProjectById(projId);
        return bn;
    }

    @RequestMapping(value="/list")
    public String init() {
        return "project/list";
    }

    @RequestMapping(value="/edit")
    public String EditProject(@RequestParam(required = false)  Integer projectId, Model model) {
        if (projectId != null && projectId > 0) {
            Project proj = projectService.getProjectById(projectId);
            model.addAttribute("proj", proj);
        }
        model.addAttribute("UserList"  ,boBoService.queryAllBoUser());
        return "project/edit";
    }

    @RequestMapping(value ="/save", method = RequestMethod.POST)
    @ResponseBody
    public Response saveBanner(ProjectForm projectForm, @RequestParam(required = false) MultipartFile file, HttpServletRequest request) {
        Project proj = projectForm.toProjectModel();
        if (file!=null) {
            FileInfo fileInfo = OssUtils.putFile(file);
            proj.setProjectImg(fileInfo.getUrl());
        }

        if (proj.getProjectId() == 0) {
            proj.setProjectId(null);
            proj.setProjectCreatetime(TimestampUtil.now());
            proj.setProjectLikecnt(0);
            proj.setProjectViewcnt(0);
            proj.setProjectVotecnt(0);
            proj.setProjectNeedmoney(0.0);
            proj.setProjectOwnmoney(0.0);
            projectService.addProject(proj);
        } else {
            projectService.updateProject(proj);
        }
        return new Response(ResponseCode.SUCC);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Response delProject(int projectId) {
        int ret = projectService.delProjectById(projectId);
        if (ret == 1) {
            return new Response(ResponseCode.SUCC);
        } else {
            return new Response(ResponseCode.ITEM_NOT_FOUND);
        }
    }

    @RequestMapping("removeFromIndex")
    @ResponseBody
    public Response removeFromIndex(Integer projectId) {
        int ret = projectService.RemoveFromIndex(projectId);
        if (ret == 1) {
            return new Response(ResponseCode.SUCC);
        } else {
            return new Response(ResponseCode.ITEM_NOT_FOUND);
        }
    }

    @RequestMapping("AddToIndex")
    @ResponseBody
    public Response AddToIndex(IndexProject projectId) {
        try {
            int ret = projectService.AddToIndex(projectId);
            if (ret == 1) {
                return new Response(ResponseCode.SUCC);
            } else {
                return new Response(ResponseCode.ERROR);
            }
        } catch (Exception e){
            return new Response(ResponseCode.ALREADY_EXIST);
        }
    }

    @RequestMapping(value = "preview")
    public String preview(@RequestParam(required = true) int projectId , Model model) {
        if(projectId > 0) {
            Project poj = projectService.getProjectById(projectId);
            model.addAttribute("proj" , poj);
            CoreUserProfile user = boBoService.queryUserProfileByUid(poj.getProjectSender(), (long) 0);
            model.addAttribute("sender", user);
        }
        return "project/preview";
    }

    @RequestMapping(value="removeActive")
    @ResponseBody
    public Response removeActive(@RequestParam(required = true) int activeId) {
        int ret = projectService.delProjActive(activeId);
        if (ret == 1) {
            return new Response(ResponseCode.SUCC);
        } else {
            return new Response(ResponseCode.ALREADY_EXIST);
        }
    }

    @RequestMapping(value = "active")
    public String ActiveMgr(@RequestParam(required = true) int projectId, Model model) {
        List<ProjectActive> pa = projectService.getProjectActiveByPId(projectId);
        model.addAttribute("pa", pa);
        model.addAttribute("projectId",projectId);
        return "project/active";
    }

    @RequestMapping(value = "getActiveById")
    @ResponseBody
    public String getActiveById(@RequestParam(required = true) int activeId) {
        ProjectActive pa = projectService.getActiveById(activeId);
        return JSON.toJSONString(pa);
    }

    @RequestMapping(value="saveActive")
    @ResponseBody
    public Response saveActive(ProjectActive pa) {
        if(pa.getActiveId() == 0) {
            pa.setCreateTime(new Date());
        }
        int ret = projectService.addProjActive(pa);
        if (ret == 1) {
            return new Response(ResponseCode.SUCC);
        } else {
            return new Response(ResponseCode.ALREADY_EXIST);
        }
    }
}
